package com.nest.WorkNest.act;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.nest.WorkNest.MyApplication;
import com.nest.WorkNest.R;

@SuppressWarnings("deprecation")
public class MainTabAct extends TabActivity {

    private Intent mHomepageIntent, mEnquiryIntent, mPlaceOrderIntent,
            mShoppingCartIntent, mMyIntent;

    private LinearLayout mBut0, mBut1, mBut2, mBut3, mBut4;

    private ImageView mIv0, mIv1, mIv2, mIv3, mIv4;
    private TextView mTv0, mTv1, mTv2, mTv3, mTv4;
    MyApplication myApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main_tab);
        myApp = (MyApplication) getApplication();
        prepareIntent();
        setupIntent();
        prepareView();
    }

    private void prepareIntent() {
        mHomepageIntent = new Intent(this, HomePageAct.class);
        mEnquiryIntent = new Intent(this, EnquiryAct.class);
        mPlaceOrderIntent = new Intent(this, PlaceOrderAct.class);
        mShoppingCartIntent = new Intent(this, ShoppingCartAct.class);
        mMyIntent = new Intent(this, MyAct.class);
    }

    private void setupIntent() {
        myApp.tabHost = getTabHost();
        myApp.tabHost.setup();

        TabSpec homepage_tab = myApp.tabHost.newTabSpec("首页")
                .setIndicator("首页").setContent(mHomepageIntent);

        TabSpec enquiry_tab = myApp.tabHost.newTabSpec("询价").setIndicator("询价")
                .setContent(mEnquiryIntent);

        TabSpec placeorder_tab = myApp.tabHost.newTabSpec("下单")
                .setIndicator("下单").setContent(mPlaceOrderIntent);

        TabSpec shoppingcart_tab = myApp.tabHost.newTabSpec("购物车")
                .setIndicator("购物车").setContent(mShoppingCartIntent);

        TabSpec my_tab = myApp.tabHost.newTabSpec("我").setIndicator("我")
                .setContent(mMyIntent);

        myApp.tabHost.addTab(homepage_tab);
        myApp.tabHost.addTab(enquiry_tab);
        myApp.tabHost.addTab(placeorder_tab);
        myApp.tabHost.addTab(shoppingcart_tab);
        myApp.tabHost.addTab(my_tab);

        myApp.tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String arg0) {

                switch (myApp.tabHost.getCurrentTab()) {
                    case 0:

                        tianchongtext(mIv0, R.drawable.tab_bar_button_home_selected);
                        tianchongColors(mTv0, R.color.tab_select);

                        tianchongtext(mIv1,
                                R.drawable.tab_bar_button_enquiry_default);
                        tianchongColors(mTv1, R.color.tab_default);

                        tianchongtext(mIv2, R.drawable.tab_bar_button_order_default);
                        tianchongColors(mTv2, R.color.tab_default);

                        tianchongtext(mIv3, R.drawable.tab_bar_button_cart_default);
                        tianchongColors(mTv3, R.color.tab_default);

                        tianchongtext(mIv4, R.drawable.tab_bar_button_my_default);
                        tianchongColors(mTv4, R.color.tab_default);

                        break;
                    case 1:

                        tianchongtext(mIv0, R.drawable.tab_bar_button_home_default);
                        tianchongColors(mTv0, R.color.tab_default);

                        tianchongtext(mIv1,
                                R.drawable.tab_bar_button_enquiry_selected);
                        tianchongColors(mTv1, R.color.tab_select);

                        tianchongtext(mIv2, R.drawable.tab_bar_button_order_default);
                        tianchongColors(mTv2, R.color.tab_default);

                        tianchongtext(mIv3, R.drawable.tab_bar_button_cart_default);
                        tianchongColors(mTv3, R.color.tab_default);

                        tianchongtext(mIv4, R.drawable.tab_bar_button_my_default);
                        tianchongColors(mTv4, R.color.tab_default);
                        break;
                    case 2:
                        tianchongtext(mIv0, R.drawable.tab_bar_button_home_default);
                        tianchongColors(mTv0, R.color.tab_default);

                        tianchongtext(mIv1,
                                R.drawable.tab_bar_button_enquiry_default);
                        tianchongColors(mTv1, R.color.tab_default);

                        tianchongtext(mIv2,
                                R.drawable.tab_bar_button_order_selected);
                        tianchongColors(mTv2, R.color.tab_select);

                        tianchongtext(mIv3, R.drawable.tab_bar_button_cart_default);
                        tianchongColors(mTv3, R.color.tab_default);

                        tianchongtext(mIv4, R.drawable.tab_bar_button_my_default);
                        tianchongColors(mTv4, R.color.tab_default);
                        break;
                    case 3:
                        tianchongtext(mIv0, R.drawable.tab_bar_button_home_default);
                        tianchongColors(mTv0, R.color.tab_default);

                        tianchongtext(mIv1,
                                R.drawable.tab_bar_button_enquiry_default);
                        tianchongColors(mTv1, R.color.tab_default);

                        tianchongtext(mIv2, R.drawable.tab_bar_button_order_default);
                        tianchongColors(mTv2, R.color.tab_default);

                        tianchongtext(mIv3, R.drawable.tab_bar_button_cart_selected);
                        tianchongColors(mTv3, R.color.tab_select);

                        tianchongtext(mIv4, R.drawable.tab_bar_button_my_default);
                        tianchongColors(mTv4, R.color.tab_default);
                        break;
                    case 4:
                        tianchongtext(mIv0, R.drawable.tab_bar_button_home_default);
                        tianchongColors(mTv0, R.color.tab_default);

                        tianchongtext(mIv1,
                                R.drawable.tab_bar_button_enquiry_default);
                        tianchongColors(mTv1, R.color.tab_default);

                        tianchongtext(mIv2, R.drawable.tab_bar_button_order_default);
                        tianchongColors(mTv2, R.color.tab_default);

                        tianchongtext(mIv3, R.drawable.tab_bar_button_cart_default);
                        tianchongColors(mTv3, R.color.tab_default);

                        tianchongtext(mIv4, R.drawable.tab_bar_button_my_selected);
                        tianchongColors(mTv4, R.color.tab_select);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    private void tianchongColors(TextView textView, int clolors) {
        textView.setTextColor(getResources().getColor(clolors));
    }

    private void tianchongtext(ImageView imageviews, int fonts) {
        imageviews.setImageResource(fonts);
        // imageviews.setBackgroundDrawable(getResources().getDrawable(fonts));
    }

    //
    private void prepareView() {

        mBut0 = (LinearLayout) findViewById(R.id.main_tab0);
        mIv0 = (ImageView) findViewById(R.id.main_tab0_iv);
        mTv0 = (TextView) findViewById(R.id.main_tab0_tv);

        mBut1 = (LinearLayout) findViewById(R.id.main_tab1);
        mIv1 = (ImageView) findViewById(R.id.main_tab1_iv);
        mTv1 = (TextView) findViewById(R.id.main_tab1_tv);

        mBut2 = (LinearLayout) findViewById(R.id.main_tab2);
        mIv2 = (ImageView) findViewById(R.id.main_tab2_iv);
        mTv2 = (TextView) findViewById(R.id.main_tab2_tv);

        mBut3 = (LinearLayout) findViewById(R.id.main_tab3);
        mIv3 = (ImageView) findViewById(R.id.main_tab3_iv);
        mTv3 = (TextView) findViewById(R.id.main_tab3_tv);

        mBut4 = (LinearLayout) findViewById(R.id.main_tab4);
        mIv4 = (ImageView) findViewById(R.id.main_tab4_iv);
        mTv4 = (TextView) findViewById(R.id.main_tab4_tv);

        tianchongtext(mIv0, R.drawable.tab_bar_button_home_selected);
        tianchongColors(mTv0, R.color.tab_select);

        MyListener myListener = new MyListener();
        mBut0.setOnClickListener(myListener);
        mBut1.setOnClickListener(myListener);
        mBut2.setOnClickListener(myListener);
        mBut3.setOnClickListener(myListener);
        mBut4.setOnClickListener(myListener);

    }

    private class MyListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.main_tab0:

                    myApp.tabHostId = 0;
                    break;
                case R.id.main_tab1:

                    myApp.tabHostId = 1;
                    break;
                case R.id.main_tab2:

                    myApp.tabHostId = 2;
                    break;
                case R.id.main_tab3:

                    myApp.tabHostId = 3;
                    break;
                case R.id.main_tab4:

                    myApp.tabHostId = 4;
                    break;
                default:
                    break;
            }
            myApp.tabHost.setCurrentTab(myApp.tabHostId);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN
                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            return true;// 消除后退键
        }
        return super.onKeyDown(keyCode, event);
    }
}
